package JDBC.tool;

import java.sql.*;

/**
 * @time: 2021/2/4/004 11:28
 * @author: Zhurunhe
 */
public class DbOpenHelper {
    private static final String CLS="com.mysql.jdbc.Driver";
    private static final String URL="jdbc:mysql://localhost:3306/test";//10.64.36.5 10.64.1.26 172.20.10.12
    private static final String USER="root";
    private static final String PWD="123456";

    public static Connection conn;//连接对象
    public static Statement stmt;//命令级
    public static PreparedStatement pstmt;//预编译命令集
    public static ResultSet rs;//结果集

    //取得连接方式
    public static void getConnection(){
        try{
            Class.forName(CLS);
            conn= DriverManager.getConnection(URL,USER,PWD);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    //关闭数据库
    public static void closeAll(){
        try{
            if(rs!=null){
                rs.close();
                rs=null;
            }
            if(stmt!=null){
                stmt.close();
                stmt=null;
            }
            if(pstmt!=null){
                pstmt.close();
                pstmt=null;
            }
            if(conn!=null){
                conn.close();
                conn=null;
            }

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
